package com.ophiux.FeverManagement.api.portImpl;

import java.lang.reflect.Type;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.FeverManagement.api.port.IPatientInfoPort;
import com.ophiux.FeverManagement.aplication.bean.doRegisterDoctorRequest;
import com.ophiux.FeverManagement.aplication.bean.doRegisterDoctorResponse;
import com.ophiux.FeverManagement.aplication.bean.patient.AddPatientInfoRequest;
import com.ophiux.FeverManagement.aplication.bean.patient.AddPatientInfoResponse;
import com.ophiux.FeverManagement.aplication.bean.patient.EditPatientInfoRequest;
import com.ophiux.FeverManagement.aplication.bean.patient.EditPatientInfoResponse;
import com.ophiux.FeverManagement.aplication.bean.patient.getPatientInfoListRequest;
import com.ophiux.FeverManagement.aplication.bean.patient.getPatientInfoListResponse;
import com.ophiux.FeverManagement.aplication.service.patient.IAddPatientInfoService;
import com.ophiux.FeverManagement.aplication.service.patient.IEditPatientInfoService;
import com.ophiux.FeverManagement.aplication.service.patient.IGetPatientInfoListService;
import com.ophiux.micro.core.application.Request;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class PatientInfoPortImpl implements IPatientInfoPort{
	@Autowired
	IAddPatientInfoService addPatientInfoService;
	
	@Autowired
	IGetPatientInfoListService getPatientInfoListService;
	@Autowired
	IEditPatientInfoService editPatientInfoService;
	@Override
	public String getPatientInfoList(String data) {
		log.info("getPatientInfoList请求参数:---->"+data);
		Result<List<getPatientInfoListResponse>> result = null;
		Request<getPatientInfoListRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<getPatientInfoListRequest>>() {
					}.getType();
				}
			});
			
			result = getPatientInfoListService.getPatientInfoList(request.getPARAM());
			
		} catch (Exception e) {
			e.printStackTrace();
			log.info("getPatientInfoList接口异常：",e);
			result = Result.fail(ResultEnum.requestException);
		}
		log.info("getPatientInfoList接口,请求参数："+data+"\n getPatientInfoList接口,返回参数："+ result.toJson());
		return result.toJson();
	}

	@Override
	public String AddPatientInfo(String data) {
		log.info("AddPatientInfo请求参数:---->"+data);
		Result<AddPatientInfoResponse> result = null;
		Request<AddPatientInfoRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<AddPatientInfoRequest>>() {
					}.getType();
				}
			});
			
			result = addPatientInfoService.AddPatientInfo(request.getPARAM());
			
		} catch (Exception e) {
			e.printStackTrace();
			log.info("AddPatientInfo接口异常：",e);
			result = Result.fail(ResultEnum.requestException);
		}
		log.info("AddPatientInfo接口,请求参数："+data+"\n AddPatientInfo接口,返回参数："+ result.toJson());
		return result.toJson();
	}
	
	@Override
	public String editPatientInfo(String data) {//editPatientInfoService
		log.info("editPatientInfo请求参数:---->"+data);
		Result<EditPatientInfoResponse> result = null;
		Request<EditPatientInfoRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<EditPatientInfoRequest>>() {
					}.getType();
				}
			});
			
			result = editPatientInfoService.editPatientInfo(request.getPARAM());
			
		} catch (Exception e) {
			e.printStackTrace();
			log.info("editPatientInfo接口异常：",e);
			result = Result.fail(ResultEnum.requestException);
		}
		log.info("editPatientInfo接口,请求参数："+data+"\n editPatientInfo接口,返回参数："+ result.toJson());
		return result.toJson();
	}

}
